package au.com.bus4u.controller.admin;


import au.com.bus4u.controller.BaseController;
import au.com.bus4u.service.AuctionService;
import au.com.bus4u.service.EmailService;
import au.com.bus4u.service.UserService;
import au.com.bus4u.utils.JsonResult;
import au.com.bus4u.utils.PageData;
import com.github.pagehelper.PageInfo;
import lombok.extern.java.Log;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/carrier")
@Log
public class CarrierController extends BaseController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private EmailService emailService;
    @Resource
    private AuctionService auctionService;
    @Resource
    private UserService userService;


    /**
     * /admin/menu/add
     */
    @RequiresPermissions("carrier:add")
    @ResponseBody
    @RequestMapping({"/add"})
    public JsonResult add() throws Exception {
        log.info("=========================================");
        JsonResult result = new JsonResult();
        userService.save(getPageData());
        result.setCode(200);
        result.setMsg("添加成功");
        return result;
    }

    @RequiresPermissions("carrier:delete")
    @ResponseBody
    @RequestMapping({"/del","/delete"})
    public JsonResult deleteMenu(String user_id) throws Exception {
        log.info("=========================================");
        JsonResult result = new JsonResult();
        userService.delete(user_id);
        result.setCode(200);
        result.setMsg("删除成功");
        return result;
    }

    @RequiresPermissions("carrier:batchdelete")
    @ResponseBody
    @RequestMapping("/batchDelete")
    public JsonResult batchDeleteGuestOrder(String[] ids) throws Exception {
        log.info("=========================================");
        for (String id : ids){
            log.info("id = [" + id + "]");
        }
//        userService.deleteAll(ids);
        JsonResult result = new JsonResult();
        result.setCode(200);
        result.setMsg("删除成功");
        return result;
    }

    @RequiresPermissions("carrier:update")
    @ResponseBody
    @RequestMapping("/update")
    public JsonResult update() throws Exception {
        log.info("=========================================");
        JsonResult result = new JsonResult();
        userService.editCarrierByUserID(getPageData());
        result.setCode(200);
        result.setMsg("修改成功");
        return result;
    }

    @RequiresPermissions("carrier:approval")
    @ResponseBody
    @RequestMapping("/changeServersStatus")
    public JsonResult changeServersStatus() throws Exception {
        log.info("=========================================");
        JsonResult result = new JsonResult();
        PageData pageData = getPageData();
        pageData.put("role_id",3);
        userService.changeServersStatus(pageData);

        //发送邮件通知司机
        String user_email = userService.getEmailByUserID(pageData.getString("user_id"));
        String subjecr = "承运商信息审批结果反馈";
        String content = "邮件内容";
        if(pageData.getInt("servers_status") == 1){
            log.info("管理审批承运商信息成功，执行邮件通知承运商服务功能已激活");
            content = "您的承运商信息完整已审批通过!";

        }
        if(pageData.getInt("servers_status") == 0){
            log.info("管理反激活承运商信息成功，执行邮件通知承运商服务功能封停");
            content = pageData.getString("email_msg");
            System.out.println("content = " + content);
        }
        emailService.sendMail(user_email,subjecr,content);


        result.setCode(200);
        result.setMsg("修改成功");
        return result;
    }

    @ResponseBody
    @RequestMapping("/goEdit")
    public JsonResult<Map> getInfoByID(String user_id) throws Exception {
        log.info("=========================================");
        PageData pd = userService.findCarrierByUserID(user_id);
        Map<String, Object> result = new HashMap<>();
        result.put("pd",pd);
        return new JsonResult<Map>(200, result);
    }


    /**
     * 查询
     */
    @RequiresPermissions("carrier:view")
    @ResponseBody
    @RequestMapping("/loadAllCarrierList")
    public PageData loadAllCarrierList() throws Exception {
        log.info("=========================================");
        setPageHelperStartPage();//从request中获取pageNum和pageSize并设置到PageHelper.startPage

        PageData pageData = getPageData();
        List<PageData> list = userService.carrierList(pageData); // 列出Category列表
        PageInfo<PageData> pageInfo = new PageInfo<>(list);//得到分页的结果对象
        List<PageData> pageList = pageInfo.getList();//获取返回页面的分页list数据
        PageData page = getPage(pageInfo);//获取分页数据page

        PageData result = new PageData();
        result.put("code",0);
        result.put("data",pageList);
        result.put("page",page);
        result.put("count",page.get("total"));//总条数 layui数据表分页组件需要

        return result;
    }


}
